package com.xc.controller;

import com.alibaba.fastjson.JSON;
import com.xc.pojo.Permission;
import com.xc.pojo.SysUser;
import com.xc.service.PermissionService;
import com.xc.service.SysUserService;
import com.xc.utils.MenuNode;
import com.xc.utils.TreeUtil;
import com.xc.vo.PermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/menu")
public class MenuController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 加载首页左侧菜单导航
     * @param permissionVo
     * @return
     * 与api/init.json对应  具体见layuimini官方文档：http://layuimini.99php.cn/docs/index.html
     */
    @RequestMapping("/loadIndexMenuLeft")
    public String loadIndexMenuLeft(PermissionVo permissionVo,Principal principal){
        //map集合保存menuInfo菜单信息
        Map<String, Object> menuInfo = new LinkedHashMap<String, Object>();
        //home集合保存homeInfo
        Map<String,Object> homeInfo = new LinkedHashMap<String, Object>();
        //logo保存logoInfo信息
        Map<String,Object> logoInfo = new LinkedHashMap<String, Object>();
        //设置只查询菜单
        permissionVo.setType("menu");
        //获取当前登入用户
        SysUser loginUser = sysUserService.queryUserByUserName(principal.getName());
        //调用查询菜单列表的方法
        List<Permission> menuList = permissionService.findPermissionListByUserId(loginUser.getId(), "menu");
        //调用查询菜单方法
//        List<Permission> menuList = permissionService.queryPermissionList(permissionVo);
        //保存集合关系
        List<MenuNode> menuNodeList=new ArrayList<MenuNode>();
        //循环遍历menuList
        for (Permission permission : menuList) {
            //创建MenuNode对象
            MenuNode menuNode = new MenuNode();
            menuNode.setId(permission.getId());//菜单编号
            menuNode.setPid(permission.getPid());//父级菜单编号
            menuNode.setTitle(permission.getTitle());//标题
            menuNode.setHref(permission.getHref());//跳转地址
            menuNode.setIcon(permission.getIcon());//菜单图标
            menuNode.setSpread(permission.getSpread());//是否展开
            menuNode.setTarget(permission.getTarget());//打开的方式
            //将menuNode添加到集合（如果直接返回menuNodeList，就全部显示了 需要动态显示）
            menuNodeList.add(menuNode);
        }
        //保存homeInfo信息
        homeInfo.put("title","首页");
        homeInfo.put("href","/admin/toYearTotalPriceManager.html");//跳转地址
        //保存logoInfo信息
        logoInfo.put("title","酒店管理系统");//logo图标
        logoInfo.put("image","/statics/layui/images/logo.png");//logo图片
        logoInfo.put("href","/index.jsp");//首页地址
        //保存menuInfo信息
        menuInfo.put("menuInfo", TreeUtil.toTree(menuNodeList,0));
        menuInfo.put("homeInfo",homeInfo);
        menuInfo.put("logoInfo",logoInfo);
        return JSON.toJSONString(menuInfo);
    }
}
